/**
 *
 * @param account
 * @param password
 *
 */
import {url} from '../util/config'
import $ from 'jquery'
import {message} from "antd";
import login from "./login";
function getNewLogin() {
    return loginToken()
}
export default function loginToken() {

    let token = localStorage.getItem("token")
    let status = null;
    $.ajax({
        headers: {
            'Accept': "application/json",
            'Authorization': token
        },
        async:false,
        url:url.api_url+`Module/GetModuleFlag`,
        success:function(result){
            console.log(result)
            if(result.result=="Token過期請重新登錄"){
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                status = getNewLogin();
            }else {
                localStorage.setItem("userType",result.userType)
                if(result.dtFirst.length>=0){
                    let arr1 = result.dtFirst.map(item => {
                        return {
                            moduleId: item.MODULE_ID,
                            moduleName: item.MODULE_NAME,
                            moduleParentId: item.MODULE_PARENT_ID,
                            moduleType: item.MODULE_TYPE,
                            moduleUrl: item.MODULE_URL,
                            sortNum:item.SORT_NUM
                        }
                    })
                    sessionStorage.setItem("getModule",JSON.stringify(arr1))
                }

                if(result.dtSecond.length>=0){
                    let arr2 = result.dtSecond.map(item => {
                        return {
                            moduleId: item.MODULE_ID,
                            moduleName: item.MODULE_NAME,
                            moduleParentId: item.MODULE_PARENT_ID,
                            moduleType: item.MODULE_TYPE,
                            moduleUrl: item.MODULE_URL
                        }
                    })
                    sessionStorage.setItem("getAdminModule",JSON.stringify(arr2))
                }

                if(result.dtThird.length>=0){
                    let arr3 = result.dtThird.map(item=>{
                        return {
                            moduleId: item.MODULE_ID,
                            moduleName: item.MODULE_NAME,
                            moduleParentId: item.MODULE_PARENT_ID,
                            moduleType: item.MODULE_TYPE,
                            moduleUrl: item.MODULE_URL,
                            sortNum:item.SORT_NUM,
                            moduleManage: item.MODULE_MANAGE_FLAG,       //   true/false
                            layoutType:item.LAYOUT_TYPE,
                            layoutArea:item.LAYOUT_AREA
                        }
                    })
                    sessionStorage.setItem("getManageModule",JSON.stringify(arr3))
                }
                status = 'ok'
            }
        },
        error: function (error) {
            message.error("请求异常");
            status = 'error'
        }
    });
    return status;
}

